<?php	require_once(dirname(__FILE__).'/inc/config.inc.php');IsModelPriv('config_var_update');

/*
**************************
(C)2010-2014 phpMyWind.com
update: 2014-5-30 10:46:30
person: Feng
**************************
*/


//初始化参数
$vargroup = isset($vargroup) ? $vargroup : 'all';
$tbname = '#@__webconfig';
$vargroupnum=$vargroup+1;
$gourl  = 'config_var.php?vargroup='.$vargroupnum;
$action = isset($action) ? $action : '';
$checkinfo = isset($checkinfo) ? $checkinfo : 'false';
	//引入操作类
require_once(ADMIN_INC.'/action.class.php');
$config_cache  = PHPMYWIND_INC.'/config.cache.php';

//更新配置函数
function WriteConfig()
{
	global $dosql, $config_cache, $gourl;
	

	$str = '<?php	if(!defined(\'IN_PHPMYWIND\')) exit(\'Request Error!\');'."\r\n\r\n";
	$dosql->Execute("SELECT `varname`,`vartype`,`varvalue`,`vargroup` FROM `#@__webconfig` where checkinfo='true' ORDER BY orderid ASC");
	while($row = $dosql->GetArray())
	{
		//强制去掉 '
		//强制去掉最后一位 / 
		$vartmp = str_replace("'",'',$row['varvalue']);

		if(substr($vartmp, -1) == '\\')
		{
			$vartmp = substr($vartmp,1,-1);
		}

		if($row['vartype'] == 'number')
		{
			if($row['varvalue'] == '')
			{
				$vartmp = 0;
			}

			$str .= "\${$row['varname']} = ".$vartmp.";\r\n";
		}
		else
		{			
			$str .= "\${$row['varname']} = '".$vartmp."';\r\n";
		}
	}
	$str .= '?>';

	if(!Writef($config_cache,$str))
	{
		ShowMsg("变量成功保存，但由于 config.cache.php 无法写入，因此不能更新配置！", $gourl);
		exit();
	}

	//RewriteURL();
}

//增加新变量
if($action == 'add')
{

	if($varname == '' || preg_match('/[^a-z_]/', $varname))
	{
		ShowMsg('变量名不能为空并必须为[a-z_]组成！', $gourl);
		exit();
	}
	
	//链接前缀
	$varname = 'cfg_'.$varname;

	if($vartype=='bool' && ($varvalue!='Y' && $varvalue!='N'))
	{
		ShowMsg('布尔变量值必须为\'Y\'或\'N\'！', $gourl);
		exit();
	}

	if($dosql->GetOne("SELECT `varname` FROM `#@__webconfig` WHERE varname='$varname'"))
	{
		ShowMsg('该变量名称已经存在！', $gourl);
		exit();
	}

	//获取OrderID
	$row = $dosql->GetOne("SELECT MAX(orderid) AS orderid FROM `#@__webconfig`");
	$orderid = $row['orderid'] + 1;

	$sql = "INSERT INTO `#@__webconfig` (siteid, varname, varinfo, varvalue, vartype, vargroup, vardesc,varcheck,orderid,checkinfo) VALUES ('$cfg_siteid', '$varname', '$varinfo', '$varvalue', '$vartype', '$vargroup', '$vardesc','$varcheck','$orderid','$checkinfo')";
	if(!$dosql->ExecNoneQuery($sql))
	{
		ShowMsg('新增变量失败，可能有非法字符！', $gourl);
		exit();
	}

		WriteConfig();
		ShowMsg('成功保存变量并更新配置文件！', $gourl);
		exit();

}

//更新变量
else if($action == 'update')
{
	$sql = "UPDATE `$tbname` SET   varinfo='$varinfo', vartype='$vartype', vargroup='$vargroup', vardesc='$vardesc', varcheck='$varcheck', orderid='$orderid', checkinfo='$checkinfo' WHERE id=$id";
		if($dosql->ExecNoneQuery($sql))
		{
			header("location:$gourl");
			exit();
		}
}


//删除变量
else if($action == 'dell')
{

	if($dosql->ExecNoneQuery("DELETE FROM `$tbname` WHERE `id`=$id"))
	{
    	header("location:$gourl");
		exit();
	}
}


//无条件返回
else
{
    header("location:$gourl");
	exit();
}
?>